<?php
namespace app\index\controller;
use think\Controller;
use Common\Controller\BaseController;
use think\Request;
use think\Session;
class identity extends Controller
{
    public function Identity()
    {
        $user = session('member');
        if (!$user) {
            header("Refresh:1;url=../../index/login/login");
        }
        $name['memberid'] = session('member.id');
        $res = db('user')->where($name)->find();
        if (!$res['memberid']) {
            return $this->fetch();
            exit;
        }
        if (!$res['name']) {
            return $this->fetch();
            exit;
        }
        if (!$res['idcardno']) {
            return $this->fetch();
            exit;
        }
        if (!$res['idcardnoaddress']) {
            return $this->fetch();
            exit;
        }
        if (!$res['bankcard']) {
            return $this->fetch();
            exit;
        }
        if (!$res['address']) {
            return $this->fetch();
            exit;
        }
        if ($res['memberid']) {
            header("Location:../../index/identity/identityimg");
            exit;
        }
        if ($res['name']) {
            header("Location:../../index/identity/identityimg");
            exit;
        }
        if ($res['idcardno']) {
            header("Location:../../index/identity/identityimg");
            exit;
        }
        if ($res['idcardnoaddress']) {
            header("Location:../../index/identity/identityimg");
            exit;
        }
        if ($res['bankcard']) {
            header("Location:../../index/identity/identityimg");
            exit;
        }
        if ($res['address']) {
            header("Location:../../index/identity/identityimg");
            exit;
        }

        return $this->fetch();
    }

    public function Identityadd()
    {
        $user = session('member');
        if (!$user) {
            header("Location:../../index/login/login");
        }
        if (!$_POST) {
            echo "<script>alert('提交为空');javascript:history.go(-1);</script>";
            //return ['code' => 0, 'msg' => '提交为空'];
        }
        //dump($_POST);exit;
        $data['name'] = trim(input('name'));
        $data['idcardno'] = trim(input('idcardno'));
        $data['idcardnoaddress'] = trim(input('idcardnoaddress'));
        $data['bankcard'] = trim(input('bankcard'));
        $data['address'] = trim(input('address'));
        $data['memberid'] = session('member.id');
        $data['phone'] = session('member.phone');
        $data['s_province'] = trim(input('s_province'));
        $data['s_city'] = trim(input('s_city'));
        $data['s_county'] = trim(input('s_county'));
        $data['weixin'] = trim(input('weixin'));
        $data['alipay'] = trim(input('alipay'));
        $this->request->post();
        $score = request()->file("score");
        $alipaydetails = request()->file("alipaydetails");

        //dump($identitycardimgjust);exit;
        $aa = $score->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
        $bb = $alipaydetails->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
//
        $a = $aa->getSaveName();//输出图片的路径及名字
        $b = $bb->getSaveName();
//
        //var_dump($a);exit;
        $data['score'] = $a;
        $data['alipaydetails'] = $b;
//
        $data['time'] = time();
        $data['examine'] =0;
        $res = db('user')->insert($data);
        if ($res) {
            header("Location:../../../index/identity/identityimg");
            exit;
        } else {
            echo "<script>alert('添加失败请检查');javascript:history.go(-1);</script>";
        }
    }

    public function identityimg()
    {
        $user = session('member');
        if (!$user) {
            header("Location:index/login/login");

        }
        $name['memberid'] = session('member.id');
        $res = db('user')->where($name)->find();
        if ($res['identitycardimgjust']) {
            header("Location:../../index/index/index");
            exit;
        }
        if ($res['identitycardimgback']) {
            header("Location:../../index/index/index");
            exit;
        }

        return $this->fetch();
    }

    public function identityimgadd()
    {
        $user = session('member');
        if (!$user) {
            header("Location:../../index/login/login");
        }
        $name['memberid'] = session('member.id');
        $ress = db('user')->where($name)->find();
        if ($ress['identitycardimgjust']) {
            echo "<script>alert('请耐心，等待审核');javascript:history.go(-1);</script>";
        }
        if ($ress['identitycardimgback']) {
            echo "<script>alert('请耐心，等待审核');javascript:history.go(-1);</script>";
        }
        $this->request->post();
        $identitycardimgjust = request()->file("identitycardimgjust");
        $identitycardimgback = request()->file("identitycardimgback");
        $personal = request()->file("personal");
        //dump($identitycardimgjust);exit;
        $aa = $identitycardimgjust->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
        $bb = $identitycardimgback->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
        $cc = $personal->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
        $a = $aa->getSaveName();//输出图片的路径及名字
        $b = $bb->getSaveName();
        $c = $cc->getSaveName();
        //var_dump($a);exit;
        $data['identitycardimgjust'] = $a;
        $data['identitycardimgback'] = $b;
        $data['personal'] = $c;
        $data['examine'] = 0;
        $res = db('user')->where($name)->update($data);
        if ($res) {
            echo "<script>alert('申请成功，等待审核');window.location.href='urgent';</script>";
        } else {
            echo "<script>alert('申请失败，请检查信息');javascript:history.go(-1);</script>";
        }


    }
    public function examine(){
        $user = session('member');
        if (!$user) {
            header("Location:../../index/login/login");
        }
        $name['memberid'] = session('member.id');
        $res = db('user')->where($name)->find();
        //dump($res['idcardno']);exit;
        if(!$res['idcardno']){
            header("Location:../../index/Identity/Identity");
            exit;
        }
        if(!$res['identitycardimgjust']){
            header("Location:../../index/Identity/Identityimg");exit;
        }
        if(!$res['relationship1']){
            header("Location:../../index/Identity/urgent");exit;
        }
        if($res['examine'] == 1){
            header("Location:../../index/loan/loan");exit;
            //echo "<script>alert('正在请耐心等待审核');javascript:history.go(-1);</script>";
        }else{
            echo "<script>alert('正在请耐心等待审核');javascript:history.go(-1);</script>";
            exit;
        }
    }

    public function examineloan(){
        $user = session('member');
        if (!$user) {
            header("Location:../../index/login/login");
        }
        $name['memberid'] = session('member.id');
        $res = db('user')->where($name)->find();
        //dump($res['idcardno']);exit;
        if(!$res['idcardno']){
            header("Location:../../index/Identity/Identity");
            exit;
        }
        if(!$res['identitycardimgjust']){
            header("Location:../../index/Identity/Identityimg");exit;
        }
        if(!$res['relationship1']){
            header("Location:../../index/Identity/urgent");exit;
        }
        if($res['examine'] == 1){
            //$this->redirect();
            header("Location:../../index/loan/bystages");exit;
            //echo "<script>alert('正在请耐心等待审核');javascript:history.go(-1);</script>";
        }else{
            echo "<script>alert('正在请耐心等待审核');window.location.href='../../../../index/index/index';</script>";
            exit;
        }
    }
public function urgent(){
    $user = session('member');
    if (!$user) {
        header("Location:../../index/login/login");
    }
    $name['memberid'] = session('member.id');
    $re = db('user')->where($name)->update();
    if($re['emergencycontact1']){
        echo "<script>alert('请耐心，等待审核');javascript:history.go(-1);</script>";
        exit;
    }
    return $this->fetch();
}
    public function urgentadd(){
        $user = session('member');
        if (!$user) {
            header("Location:../../index/login/login");
        }
        $name['memberid'] = session('member.id');
        $data['emergencycontact1'] = trim(input('emergencycontact1'));
        $data['relationship1'] = trim(input('relationship1'));
        $data['emergencycontact2'] = trim(input('emergencycontact2'));
        $data['relationship2'] = trim(input('relationship2'));
        $data['emergencycontact3'] = trim(input('emergencycontact3'));
        $data['relationship3'] = trim(input('relationship3'));
        $data['name1'] = trim(input('name1'));
        $data['name2'] = trim(input('name2'));
        $data['name3'] = trim(input('name3'));
        //dump($_POST);exit;
        $res = db('user')->where($name)->update($data);
        if($res){
            echo "<script>alert('已提交，请等待审核');window.location.href='../../../../index/bankcard/bankcard_insa';</script>";
        }else{
            echo "<script>alert('提交失败，请检查内容');javascript:history.go(-1);</script>";
        }
    }
}